<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="是否放号：">
            <el-select v-model="listQuery.isOpen" placeholder="全部" clearable class="input-width">
              <el-option v-for="item in statusOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="申领日期：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.applyDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="returnApplyTable"
                :data="list"
                style="width: 100%;"
                @selection-change="handleSelectionChange"
                v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"  disabled="scope.row.hasOrderNumber === 0"></el-table-column>
        <el-table-column label="申领日期" width="180" align="center">
          <template slot-scope="scope">{{scope.row.applyDate}}</template>
        </el-table-column>
        <el-table-column label="已送达数量" width="180" align="center">
          <template slot-scope="scope">
          <span @click="handleClick(scope.row,1)">{{scope.row.hasOrderNumber}}</span>
          <span @click="handleClick(scope.row,1)" style="color: dodgerblue">查看详情</span>
          </template>
        </el-table-column>
        <el-table-column label="未送达数量" align="center">
          <template slot-scope="scope">
            <span @click="handleClick(scope.row,0)">{{scope.row.unOrderNumber}}</span>
            <span @click="handleClick(scope.row,0)" style="color: dodgerblue">查看详情</span>
          </template>
        </el-table-column>
        <el-table-column label="是否放号" width="180" align="center">
          <template slot-scope="scope">{{scope.row.isOpen | formatStatus}}</template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operateOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog title="车牌详情" :visible.sync="selectDialogVisible" width="50%">
      <el-input v-model="dialogData.listQuery.plateNumber"
                style="width: 250px;margin-bottom: 20px"
                size="small"
                placeholder="车牌号搜索">
        <el-button slot="append" icon="el-icon-search" @click="handleSelectSearch()"></el-button>
      </el-input>
      <el-table :data="dialogData.list"
                @selection-change="handleDialogSelectionChange" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="车牌号" align="center" >
          <template slot-scope="scope">{{scope.row.plateNumber}}</template>
        </el-table-column>
        <el-table-column label="车牌类型" width="160" align="center">
          <template slot-scope="scope">{{scope.row.plateType}}</template>
        </el-table-column>
        <el-table-column label="申领日期" width="160" align="center">
          <template slot-scope="scope">{{scope.row.applyDate}}</template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleDialogSizeChange"
          @current-change="handleDialogCurrentChange"
          layout="prev, pager, next"
          :current-page.sync="dialogData.listQuery.pageNum"
          :page-size="dialogData.listQuery.pageSize"
          :page-sizes="[5,10,15]"
          :total="dialogData.total">
        </el-pagination>
      </div>
      <div style="clear: both;"></div>
      <div slot="footer">
        <el-button  size="small" @click="selectDialogVisible = false">取 消</el-button>
        <el-button  size="small" type="primary" @click="handleSelectDialogConfirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {formatDate} from '@/utils/date';
import {fetchList,deleteApply} from '@/api/returnApply';
import {fetchPlateList} from '@/api/productCate'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  id: null,
  receiverKeyword: null,
  status: null,
  createTime: null,
  handleMan: null,
  handleTime: null
};
const defaultStatusOptions=[
  {
    label: '未放号',
    value: false
  },
  {
    label: '已放号',
    value: true
  }
];
export default {
  name:'returnApplyList',
  data() {
    return {
      selectDialogVisible:false,
      listQuery:Object.assign({},defaultListQuery),
      statusOptions: Object.assign({},defaultStatusOptions),
      list:null,
      total:null,
      listLoading:false,
      multipleSelection:[],
      operateType:1,
      operateOptions: [
        {
          label: "批量放号",
          value: 1
        }
      ],
      dialogData:{
        list: null,
        total: null,
        multipleSelection:[],
        listQuery:{
          applyDate:null,
          plateNumber: null,
          pageNum: 1,
          pageSize: 5,
          type:1
        }
      },
    }
  },
  created(){
    this.getList();
  },
  filters:{
    formatTime(time) {
      if(time==null||time===''){
        return 'N/A';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    },
    formatStatus(status){
      for(let i=0;i<defaultStatusOptions.length;i++){
        if(status===defaultStatusOptions[i].value){
          return defaultStatusOptions[i].label;
        }
      }
    }
  },
  methods:{
    handleClick(row,type){
      this.selectDialogVisible=true;
      this.dialogData.listQuery.plateNumber=null;
      this.dialogData.listQuery.type=type;
      this.dialogData.listQuery.applyDate=row.applyDate;
      this.getDialogList();

    },
    getDialogList() {
      this.listLoading = true;
      fetchPlateList(this.dialogData.listQuery).then(response => {
        this.listLoading = false;
        this.dialogData.list = response.data.records;
        this.dialogData.total = response.data.total;
      });
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleBatchOperate(){
      if(this.multipleSelection==null||this.multipleSelection.length<1){
        this.$message({
          message: '请选择要操作的申请',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      if(this.operateType===1){
        //批量删除
        this.$confirm('是否要进行放号操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = new URLSearchParams();
          let dates=[];
          for(let i=0;i<this.multipleSelection.length;i++){
            dates.push(this.multipleSelection[i].applyDate);
          }
          params.append("dates",dates);
          deleteApply(params).then(response=>{
            this.getList();
            this.$message({
              type: 'success',
              message: '开放成功!'
            });
          });
        })
      }
    },
    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },
    getList(){
      this.listLoading=true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
      });
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


